package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotDeviceSensorRelService;
import com.lechi.web.system.domain.IotDeviceSensorBo;
import com.lechi.web.system.domain.IotDeviceSensorRelBo;
import com.lechi.web.system.domain.IotDeviceSensorsBo;
import com.lechi.web.system.mapper.iot.IotDeviceSensorRelMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 设备关联观测要素关系Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotDeviceSensorRelServiceImpl implements IIotDeviceSensorRelService {

    private final IotDeviceSensorRelMapper iotDeviceSensorRelMapper;

    /**
     * 查询设备关联观测要素关系列表
     *
     * @param iotDeviceSensorRelBo 设备关联观测要素关系
     * @return 设备关联观测要素关系
     */
    @Override
    public List<IotDeviceSensorBo> selectIotDeviceSensorRelList(IotDeviceSensorRelBo iotDeviceSensorRelBo) {
        try {
            return iotDeviceSensorRelMapper.selectIotDeviceSensorRelList(iotDeviceSensorRelBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询设备关联观测要素关系列表", method, iotDeviceSensorRelBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询设备关联观测要素关系列表
     *
     * @param iotDeviceSensorRelBo 设备关联观测要素关系
     * @return 设备关联观测要素关系
     */
    @Override
    public List<IotDeviceSensorRelBo> selectIotDeviceSensorRelListByDeviceId(IotDeviceSensorRelBo iotDeviceSensorRelBo) {
        try {
            return iotDeviceSensorRelMapper.selectIotDeviceSensorRelListByDeviceId(iotDeviceSensorRelBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过设备ID查询设备关联观测要素关系列表", method, iotDeviceSensorRelBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询设备关联观测要素关系列表
     *
     * @param iotDeviceSensorRelBo 设备关联观测要素关系
     * @return 设备关联观测要素关系
     */
    @Override
    public List<IotDeviceSensorsBo> selectByDeviceIdAndSensorId(IotDeviceSensorRelBo iotDeviceSensorRelBo) {
        try {
            return iotDeviceSensorRelMapper.selectByDeviceIdAndSensorId(iotDeviceSensorRelBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过设备ID和要素ID查询设备关联观测要素关系列表", method, iotDeviceSensorRelBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增设备关联观测要素关系
     *
     * @param iotDeviceSensorRelBo 设备关联观测要素关系
     */
    @Override
    public void insertIotDeviceSensorRel(IotDeviceSensorRelBo iotDeviceSensorRelBo) {
        try {
            iotDeviceSensorRelMapper.insertIotDeviceSensorRel(iotDeviceSensorRelBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增设备关联观测要素关系", method, iotDeviceSensorRelBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改设备关联观测要素关系
     *
     * @param iotDeviceSensorRelBo 设备关联观测要素关系
     */
    @Override
    public void updateIotDeviceSensorRel(IotDeviceSensorRelBo iotDeviceSensorRelBo) {
        try {
            iotDeviceSensorRelMapper.updateIotDeviceSensorRel(iotDeviceSensorRelBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改设备关联观测要素关系", method, iotDeviceSensorRelBo));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 批量删除设备关联观测要素关系
     *
     * @param ids 需要删除的设备关联观测要素关系主键
     */
    @Override
    public void deleteIotDeviceSensorRelByIds(Long[] ids) {
        try {
            iotDeviceSensorRelMapper.deleteIotDeviceSensorRelByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTrace = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTrace.getClassName() + "." + stackTrace.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除设备关联观测要素关系信息", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 删除设备关联观测要素关系信息
     *
     * @param id 设备关联观测要素关系主键
     */
    @Override
    public void deleteIotDeviceSensorRelByDeviceId(Long id) {
        try {
            iotDeviceSensorRelMapper.deleteIotDeviceSensorRelByDeviceId(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过设备ID删除设备关联观测要素关系信息", method, id));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
